package com.xyzq.webapp.action.ya.data;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzq.model.common.DataDictionary;
import com.xyzq.model.ya.data.Account;
import com.xyzq.model.ya.data.Staff;
import com.xyzq.service.common.DataDictionaryManager;
import com.xyzq.service.ya.AccountManager;
import com.xyzq.service.ya.StaffManager;
import com.xyzq.webapp.action.BaseAction;

public class AccountAction extends BaseAction implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1906015758141636923L;
	private Long id;
	private Integer accountId;
	private Account account;
	private List<DataDictionary> accountBanks;
	private List<DataDictionary> accountTransfers;
	private List<Staff> staffs;
	private List<Account> accountList;
	private AccountManager accountManager;
	private DataDictionaryManager dataDictionaryManager;
	private StaffManager staffManager;

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		this.accountBanks = this.dataDictionaryManager
				.findByModule("accountBank");
		this.accountTransfers = this.dataDictionaryManager
				.findByModule("accountTransfer");
		this.staffs = this.staffManager.getAll();
	}
	
	public String accountDetailWithAssert() {
		this.setAccount(this.getAccountManager()
				.findByAccountId(getAccountId()));
		return SUCCESS;
	}

	public String list() {
		this.accountList = this.accountManager.getAll();
		this.logUserAction("AccountAction", "read all list");
		return SUCCESS;
	}

	public String edit() {
		if (this.id != null) {
			account = this.accountManager.get(id);
			this.logUserAction("AccountAction", "ready edit:"+this.account);
		} else {
			account = new Account();
			this.logUserAction("AccountAction", "ready input:"+this.account);
		}
		return SUCCESS;
	}

	public String save() {
		if (cancel != null) {
			return CANCEL;
		}
		boolean isNew = (this.account.getId() == null);
		if (isNew) {
			this.account.setSubTime(new Date());
		} else {
			this.account.setSubTime(this.accountManager.get(
					this.account.getId()).getSubTime());
			this.account.setLastModifyTime(new Date());
		}
		this.accountManager.save(account);
		String key = (isNew) ? "account.added" : "account.updated";
		saveMessage(getText(key));
		if (!isNew) {
			this.logUserAction("AccountAction", "edit:"+this.account);
			return INPUT;
		} else {
			this.logUserAction("AccountAction", "save:"+this.account);
			return SUCCESS;
		}
	}

	public Long getId() {
		return id;
	}

	public Account getAccount() {
		return account;
	}

	public List<DataDictionary> getAccountBanks() {
		return accountBanks;
	}

	public List<DataDictionary> getAccountTransfers() {
		return accountTransfers;
	}

	public List<Staff> getStaffs() {
		return staffs;
	}

	public List<Account> getAccountList() {
		return accountList;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public DataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public StaffManager getStaffManager() {
		return staffManager;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public void setAccountBanks(List<DataDictionary> accountBanks) {
		this.accountBanks = accountBanks;
	}

	public void setAccountTransfers(List<DataDictionary> accountTransfers) {
		this.accountTransfers = accountTransfers;
	}

	public void setStaffs(List<Staff> staffs) {
		this.staffs = staffs;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public void setDataDictionaryManager(
			DataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public void setStaffManager(StaffManager staffManager) {
		this.staffManager = staffManager;
	}

	public void setAccountId(Integer accountId) {
		this.accountId = accountId;
	}

	public Integer getAccountId() {
		return accountId;
	}

}
